package com.yanqu.road.server.cmd.activity.rankunionwar;

import com.yanqu.road.entity.activity.rankunionwar.config.RankUnionWarConfig;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.RankUnionWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.rankunionwar.RankUnionWarMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_RANKUNIONWAR_APPOINT_COMMANDER, desc = "")
public class RankUnionWarAppointCommanderCmd extends AbstractCommand {

    RankUnionWarProto.RankUnionWarAppointCommanderReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = RankUnionWarProto.RankUnionWarAppointCommanderReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        RankUnionWarConfig config = RankUnionWarMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId());
        if(userBaseInfo == null){
            ret = GameErrorCode.E_RANKUNIONWAR_NOABLE_APPOINT_COMMANDER;
            return;
        }
        CrossUnionMember unionMember = CrossUnionMgr.getUnionMember(userBaseInfo.getUnionUid(),player.getUserId());
        if(unionMember == null){
            ret = GameErrorCode.E_RANKUNIONWAR_NOABLE_APPOINT_COMMANDER;
            return;
        }
        if(unionMember.getPosition() != eUnionPosition.Master.getValue() && unionMember.getPosition() != eUnionPosition.DeputyMaster.getValue()){
            ret = GameErrorCode.E_RANKUNIONWAR_NOABLE_APPOINT_COMMANDER;
            return;
        }

        player.sendPacket(CrossProtocol.C2_CROSS_RANKUNIONWAR_APPOINT_COMMANDER, reqMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }
        RankUnionWarProto.RankUnionWarAppointCommanderRespMsg.Builder respMsg = RankUnionWarProto.RankUnionWarAppointCommanderRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_RANKUNIONWAR_APPOINT_COMMANDER, respMsg);
    }
}
